﻿Partial Public Class PhotosPage2

    Public Sub New()
        InitializeComponent()
        MyInitialize()
    End Sub

    Private Sub MyInitialize()
        MapViewControl1.Connection = g_connection
        MapViewControl1.AlbumManager = g_album_mgr
        MapViewControl1.ShowControlsOnMap = False
        AlbumSelectorControl1.Connection = g_connection
        AlbumSelectorControl1.AlbumManager = g_album_mgr
        AlbumSelectorControl1.MapControl = MapViewControl1
    End Sub

    Private Sub VirtualEarthMapControl1_PhotoClicked(ByVal VE_shapeid As String, ByVal row As System.Data.DataRow) Handles MapViewControl1.PhotoClicked
        g_currentphoto.Row = row
        'MessageBox.Show("click: " & VE_shapeid)
    End Sub

    Private Sub VirtualEarthMapControl1_PhotoDoubleClicked(ByVal VE_shapeid As String, ByVal row As System.Data.DataRow) Handles MapViewControl1.PhotoDoubleClicked
        'MessageBox.Show("doubleclick: " & VE_shapeid)
        ImageViewerForm.ShowFullImage(g_currentphoto.Row)
    End Sub

    Private m_moving As Border = Nothing
    Private m_start_element As Point = Nothing
    Private m_start_point As Point = Nothing
    Private m_debug_moving As Boolean = False

    Private Sub Canvas1_GotMouseCapture(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Canvas1.GotMouseCapture
        If m_debug_moving Then Trace.WriteLine("Canvas1_GotMouseCapture")
    End Sub

    Private Sub MoveToTopLeft(ByVal b As Border)
        Canvas.SetTop(b, 0)
        Canvas.SetLeft(b, 0)
    End Sub

    Private Sub MoveToBottomRight(ByVal b As Border)
        Canvas.SetTop(b, Canvas1.ActualHeight - b.ActualHeight)
        Canvas.SetLeft(b, Canvas1.ActualWidth - b.ActualWidth)
    End Sub

    Private Sub Canvas1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Canvas1.MouseDown
        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseDown")

        m_start_point = e.GetPosition(Canvas1)
        Dim dblclick As Boolean = (e.ClickCount = 2 And e.ButtonState = MouseButtonState.Released)

        m_moving = Nothing
        For Each ui As UIElement In Canvas1.Children.OfType(Of MyBorder)()
            If Not ui.IsMouseOver Then Continue For
            If m_moving Is Nothing Then m_moving = ui
            If Canvas.GetZIndex(ui) > Canvas.GetZIndex(m_moving) Then m_moving = ui
        Next

        If m_moving Is Nothing Then
            If m_debug_moving Then Trace.WriteLine("Canvas1_MouseDown exit (nothing)")
            Return
        End If

        ' stop any animations from overriding the Top and Left properties
        m_moving.BeginAnimation(Canvas.TopProperty, Nothing)
        m_moving.BeginAnimation(Canvas.LeftProperty, Nothing)


        If dblclick Then
            'If e.ChangedButton = MouseButton.Left Then
            '    MoveToTopLeft(m_moving)
            'ElseIf e.ChangedButton = MouseButton.Right Then
            '    MoveToBottomRight(m_moving)
            'End If
        Else
            m_start_element = New Point(Canvas.GetLeft(m_moving), Canvas.GetTop(m_moving))
            m_moving.CaptureMouse()
        End If

        For Each o As Object In Canvas1.Children
            Canvas.SetZIndex(o, 0)
        Next
        Canvas.SetZIndex(m_moving, 1)

        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseDown startpoint " & m_start_point.ToString())
        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseDown element " & m_start_element.ToString())
        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseDown exit")
        e.Handled = True

    End Sub

    Private Sub Canvas1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Canvas1.MouseUp
        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseUp")
        If m_moving Is Nothing Then Return
        m_moving.ReleaseMouseCapture()
        m_moving = Nothing
    End Sub

    Private Sub Canvas1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Canvas1.MouseMove

        Static i As Integer = 0 : i += 1
        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseMove " & i)
        If m_moving Is Nothing Then Trace.WriteLine("Canvas1_MouseMove exit (nothing) " & i)
        If m_moving Is Nothing Then Return
        Dim newpos As Point = e.GetPosition(Canvas1) - m_start_point + m_start_element
        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseMove getpos " & e.GetPosition(Canvas1).ToString)
        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseMove newpos " & newpos.ToString)


        ' constrain the child's new position to the bounds of the canvas
        If newpos.X + m_moving.ActualWidth > Canvas1.ActualWidth Then newpos.X = Canvas1.ActualWidth - m_moving.ActualWidth
        If newpos.X < 0 Then newpos.X = 0
        If newpos.Y + m_moving.ActualHeight > Canvas1.ActualHeight Then newpos.Y = Canvas1.ActualHeight - m_moving.ActualHeight
        If newpos.Y < 0 Then newpos.Y = 0

        If newpos.X = Canvas.GetLeft(m_moving) AndAlso newpos.Y = Canvas.GetTop(m_moving) Then Return

        Canvas.SetLeft(m_moving, newpos.X)
        Canvas.SetTop(m_moving, newpos.Y)
        'm_moving.RenderTransform = New TranslateTransform(newpos.X, newpos.Y)
        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseMove trans " & m_moving.RenderTransform.Value.ToString)
        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseMove exit " & i)
    End Sub



    Private Sub Canvas1_SizeChanged(ByVal sender As Object, ByVal e As System.Windows.SizeChangedEventArgs) Handles Canvas1.SizeChanged
        'MapViewControl1.Width = e.NewSize.Width * 0.75
        'MapViewControl1.Height = e.NewSize.Height * 0.75
    End Sub

    Private Sub Canvas1_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Canvas1.MouseLeave
        If m_debug_moving Then Trace.WriteLine("Canvas1_MouseLeave")
        m_moving = Nothing
    End Sub

    Private Sub Canvas1_LostMouseCapture(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Canvas1.LostMouseCapture
        If m_debug_moving Then Trace.WriteLine("Canvas1_LostMouseCapture")
    End Sub
End Class
